package com.lijin.study.java.thread.task;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;


public class ReadFileTask implements Task {

	private String fileName;
	
	public ReadFileTask(String fileName) {
		this.fileName = fileName;
	}
	
	@Override
	public void execute() {
		System.out.println("读取文件【" + fileName + "】开始");
		int lineCnt = 0;
		File f = new File(fileName);
		if (f.isFile()) {
			BufferedReader br = null;
			try {
				br = new BufferedReader(new FileReader(f));
				while (br.readLine() != null) {
					lineCnt ++;
				}
			} catch (Exception e) {
				System.err.println("读取文件【" + fileName + "】出错");
			} finally {
				if (br != null) {
					try {
						br.close();
					} catch (Exception e) {
						System.err.println("关闭文件【" + fileName + "】的读入流失败");
						e.printStackTrace();
					}
				}
			}
		}
		System.out.println("读取文件【" + fileName + "】完成，共【" + lineCnt + "】行数据");
	}
}
